package com.qf.emp.jsp;

import com.qf.emp.entity.Emp;
import com.qf.emp.entity.EmpManager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ShowAllEmpJSP",value = "/manager/safe/showAllEmpJSP")
public class ShowAllEmpJSP extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        1. 获取集合数据
//        EmpManager empManager = (EmpManager) request.getAttribute("empManager");
////        System.out.println(empManager.getUsername());
////        System.out.println(empManager.getPassword());

        List<Emp> emps = (List<Emp>) request.getAttribute("emps");

        PrintWriter printWriter = response.getWriter();

        if(emps!=null){
            printWriter.println("<html>");
            printWriter.println("<head>");
            printWriter.println("<meta charset='UTF-8'>");
            printWriter.println("<title>结果页面</title>");
            printWriter.println("</head>");
            printWriter.println("<body>");
            printWriter.println("<table border='1'>");
            printWriter.println("   <tr>");
            printWriter.println("       <td>编号</td>");
            printWriter.println("       <td>姓名</td>");
            printWriter.println("       <td>工资</td>");
            printWriter.println("       <td>年龄</td>");
            printWriter.println("       <td colspan='2'>操作</td>");
            printWriter.println("   </tr>");
            for (Emp emp:emps){
                printWriter.println("   <tr>");
                printWriter.println("       <td>"+emp.getId()+"</td>");
                printWriter.println("       <td>"+emp.getName()+"</td>");
                printWriter.println("       <td>"+emp.getSalary()+"</td>");
                printWriter.println("       <td>"+emp.getAge()+"</td>");
                printWriter.println("       <td><a href='"+ request.getContextPath() + "/manager/safe/removeEmpController?id=" + emp.getId() +"'>删除</a></td>");
                printWriter.println("       <td><a href='"+ request.getContextPath() + "/manager/safe/showEmpController?id=" + emp.getId() +"'>修改</a></td>");
                printWriter.println("   </tr>");
            }
            printWriter.println("</table>");
            printWriter.println("</body>");
            printWriter.println("</html>");
        }else{
            printWriter.println("<html>");
            printWriter.println("<head>");
            printWriter.println("<meta charset='UTF-8'>");
            printWriter.println("<title>结果页面</title>");
            printWriter.println("</head>");
            printWriter.println("<body>");
            printWriter.println("<h3>当前没有用户数据!</h3>");
            printWriter.println("</body>");
            printWriter.println("</html>");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
